<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>1.图片边缘模糊效果</title>
    <style>
        canvas{
            border:1px solid #aaa;
            margin: 0 auto;
            display: block;
        }
    </style>
</head>
<body>
    <canvas width="0" height="0"></canvas>

    <script type="module">

        import { loadImage, getImageData, traverse, gaussianBlur } from "/common/lib/utils.js"
        import { transformColor, multiply, grayscale, brightness, saturate, contrast } from "/common/lib/color-matrix.js"

        const canvas = document.querySelector('canvas'); 
        const context = canvas.getContext('2d');

        (async function (){
            // 异步加载图片
            let img = await loadImage('https://p2.ssl.qhimg.com/d/inn/4b7e384c55dc/girl1.jpg');
            
            // 获取图片剪裁区的数据 imgData（包含了图片的所有像素数据）
            const imageData = getImageData(img);

            // 循环遍历 imageData 数据，并处理
            traverse(imageData,({r, g, b, a, x, y}) => { 
                const d = Math.hypot((x - 0.5), (y - 0.5)); 
                a *= 1.0 - 2 * d; 
                return [r, g, b, a];
            });
            canvas.width = imageData.width;
            canvas.height = imageData.height;
            context.putImageData(imageData, 0, 0);
        })();
        
    </script>
</body>
</html>